﻿Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraEditors.Controls
Imports System.Threading
Imports System.Data.OleDb
Imports System.Windows.Forms
Imports System.Drawing

Public Class clsConexao
    Public Sub New()

    End Sub
    Public Sub New(ByVal parClsPDVInt As clsPDVINT)
        pClsPDVINT = parClsPDVInt
    End Sub
    Private fRegistroNovo As Boolean
    Public Property RegistroNovo() As Boolean
        Get
            Return fRegistroNovo
        End Get
        Set(ByVal value As Boolean)
            fRegistroNovo = value
        End Set
    End Property

    Enum enuTipoRegistro
        eRegSeparacao = 1
        eRegRemanejamento = 2
        eRegPre_Venda = 3
        eRegKit = 4
        eRegConhecimento_Transporte = 5
        eRegPedido_Compra = 6
    End Enum
    Enum enuTConcatena
        Where = 0
        EAnd = 1
        EOr = 2
        EBT = 3
    End Enum
    Enum enuTPlic
        Values = 0
        Virgula = 1
        Parentes = 2
        Nenhum = 3
    End Enum
    Enum enuTComparacao
        Igual = 0
        Diferente = 1
        eDN = 2
    End Enum
    Enum enuTConexao
        Consulta = 0
        Atualizacao = 1
        Assyncrono = 2
    End Enum
    Event ListaCampo(ByVal ParAcao As enuTConexao, ByRef parErro As Boolean)
    Public fAlterou As Boolean, fBanco As New ADODB.Connection, fConectionString As String
    Public fNomeTabela As String
    Private fCondicao As String, fDyna As New ADODB.Recordset, fTipoAcao As Byte, fSql As String
    Private fProcessando As Boolean
    Public Property Processando() As Boolean
        Get
            Return fProcessando
        End Get
        Set(ByVal value As Boolean)
            fProcessando = value
        End Set
    End Property
    Public Property Dyna() As ADODB.Recordset
        Get
            Return fDyna
        End Get
        Set(ByVal value As ADODB.Recordset)
            fDyna = value
        End Set
    End Property

    Private fTransacao As Boolean
    Public Property Transacao() As Boolean
        Get
            Return fTransacao
        End Get
        Set(ByVal value As Boolean)
            fTransacao = value
        End Set
    End Property
    Public Function TransacaoAbre() As Boolean
        Try
            If Not Transacao Then
                fBanco.BeginTrans()
                Transacao = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Return False
        End Try
    End Function
    Public Function TransacaoConfirma() As Boolean
        Try
            fBanco.CommitTrans()
            Transacao = False
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Return False
        End Try
    End Function
    Public Function TransacaoCancela() As Boolean
        Try
            If Transacao Then fBanco.RollbackTrans()
            Transacao = False
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Return False
        End Try
    End Function
    Public Property CondicaoSql() As String
        Get
            Return fCondicao
        End Get
        Set(ByVal value As String)
            fCondicao = value
        End Set
    End Property
    Public Overloads Function PegarNovo(ByRef parRetorno As String, ByVal parCampo As String, ByVal parTamanho As Byte, Optional ByVal parCondicao As String = "") As Boolean
        Dim sRetorno As Integer
        If PegarNovo(sRetorno, parCampo, parCondicao) Then
            parRetorno = sRetorno.ToString.PadLeft(parTamanho, "0")
            Return True
        Else
            Return False
        End If
    End Function
    Public Overloads Function PegarNovo(ByRef parRetorno As Integer, ByVal parCampo As String, Optional ByVal parCondicao As String = "") As Boolean
        Dim sSql As String, sDyna As New ADODB.Recordset
        parRetorno = 0

        Try
            sSql = "SELECT MAX(" & parCampo & ") FROM " & fNomeTabela
            If parCondicao <> "" Then sSql &= " WHERE " & parCondicao
            If AbreConexao(sDyna, sSql, enuTConexao.Consulta) Then
                With sDyna
                    If Not .EOF Then
                        parRetorno = Val(IIf(IsDBNull(.Fields(0).Value), 0, .Fields(0).Value))
                    End If
                    parRetorno += 1
                End With
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Return False
        Finally
            Libera(sDyna)
        End Try
    End Function
    Public Function ProcessarConsulta() As Boolean
        Dim sErro As Boolean
        RaiseEvent ListaCampo(enuTConexao.Consulta, sErro)
        Return Not sErro
    End Function
    Public Function ProcessarSql() As String
        fTipoAcao = 2
        Dim sErro As Boolean
        RaiseEvent ListaCampo(fTipoAcao, sErro)
        If fCondicao Is Nothing OrElse fCondicao = "" Then
            MsgBox("A classe está sem chave primária!")
            Return ""
            Exit Function
        End If
        Return "SELECT " & fSql.Substring(1) & " FROM " & fNomeTabela & " WHERE " & fCondicao.Substring(5)
    End Function
    Public Function Processar(ByVal parAcao As enuTConexao) As Boolean
        fRegistroNovo = False
        fSql = ""
        CondicaoSql = ""
        fTipoAcao = 2
        Dim sErro As Boolean
        RaiseEvent ListaCampo(fTipoAcao, sErro)
        If Conecta() Then
            Try
                If fCondicao Is Nothing OrElse fCondicao = "" Then
                    MsgBox("A classe está sem chave primária!")
                    Return False
                    Exit Function
                End If
                If AbreConexao(fDyna, "SELECT " & fSql.Substring(1) & " FROM " & fNomeTabela & " WHERE " & fCondicao.Substring(5), parAcao) Then
                    fTipoAcao = parAcao
                    If parAcao = enuTConexao.Atualizacao Then
                        If fDyna.EOF Then
                            fRegistroNovo = True
                            fDyna.AddNew()
                        End If
                        RaiseEvent ListaCampo(parAcao, sErro)
                        fDyna.Update()
                    Else
                        RaiseEvent ListaCampo(parAcao, sErro)
                        Libera(fDyna)
                    End If
                    Return True
                Else
                    Return False
                End If
            Catch ex As Exception
                GravaErro(Err, ex)
                If parAcao = enuTConexao.Atualizacao Then fDyna.CancelUpdate()
                Return False
            Finally
                Libera(fDyna)
                Desconecta()
            End Try
        Else
            Return False
        End If
    End Function
    Public Sub AlimentaCampo(ByVal parCampo As String, ByRef parObjeto As Object, Optional ByVal parCondicao As Boolean = False, Optional ByVal parCryptografado As Boolean = False, Optional ByVal parUcase As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        Select Case fTipoAcao
            Case 2
                fSql &= "," & parCampo
                If parCondicao Then fCondicao &= Plic(parCampo, enuTComparacao.Igual, enuTConcatena.EAnd, parObjeto)
            Case 0
                Recebe(parObjeto, fDyna, parCampo, parCryptografado, parSemTrim)
            Case 1
                If parUcase Then parObjeto = parObjeto.ToString.ToUpper
                Passa(parObjeto, fDyna, parCampo, parCryptografado)
        End Select
    End Sub
    Public Sub Libera(ByRef parDyna As ADODB.Recordset)
        On Error GoTo Trata
        With parDyna
            If .State = 1 Then .Close()
            'parDyna = Nothing
            .MaxRecords = 0
        End With
        Exit Sub
Trata:
        If Err.Number = 3219 Or Err.Number = 3705 Then
            Resume Next
        Else
            MsgBox(Err.Description, 64, "Libera")
        End If
    End Sub
    Public Function SetBanco(ByVal parServidor As String, ByVal parBanco As String, ByVal parUsuario As String, ByVal parSenha As String) As Boolean
        Try
            With fBanco
                .Provider = "SQLOLEDB"
                .Properties("Data Source").Value = parServidor
                .Properties("Initial Catalog").Value = parBanco
                .Properties("User Id").Value = parUsuario
                .Properties("Password").Value = parSenha
                .Properties("Persist Security Info").Value = True
                .CommandTimeout = 900000
                .Open()
                fConectionString = .ConnectionString
                .Close()
            End With
            Return True
        Catch ex As Exception
            Return False
            GravaErro(Err, ex)
        End Try
    End Function
    Public Function AbreConexao(ByRef parDyna As ADODB.Recordset, ByVal parSQl As String, ByVal parTipo As enuTConexao) As Boolean
        Try
            Libera(parDyna)
            parDyna = New ADODB.Recordset
            parDyna.CursorLocation = ADODB.CursorLocationEnum.adUseClient
            If parTipo = enuTConexao.Consulta Then
Novamente:
                parDyna.Open(parSQl, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, )
                If parDyna.EOF Then
                    Return False
                    Libera(parDyna)
                Else
                    Return True
                End If
            ElseIf parTipo = enuTConexao.Assyncrono Then
                Dim sComando As ADODB.Command
                sComando = New ADODB.Command
                sComando.Prepared = False
                sComando.ActiveConnection = fBanco
                sComando.CommandType = ADODB.CommandTypeEnum.adCmdText
                sComando.CommandText = parSQl
                sComando.CommandTimeout = 900
                parDyna = sComando.Execute(, , ADODB.ExecuteOptionEnum.adAsyncExecute)
                While sComando.State <> 0
                    Application.DoEvents()
                End While
            Else
                parDyna.Open(parSQl, fBanco, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
                Return True
            End If
        Catch ex As Exception
            If Err.Description.ToUpper.IndexOf("excedido".ToUpper) > 0 Then
                GoTo Novamente
            Else
                GravaErro(Err, ex, parSQl)
            End If
            Return False
        End Try
    End Function
    Public Overloads Function CarregaConfig(ByVal parServidor As String, ByVal parBanco As String, ByVal parUsuario As String, ByVal parSenha As String) As Boolean
        Try
            With fBanco
                .Provider = "SQLOLEDB"
                .Properties("Data Source").Value = parServidor
                .Properties("Initial Catalog").Value = parBanco
                .Properties("User Id").Value = parUsuario
                .Properties("Password").Value = parSenha
                .Properties("Persist Security Info").Value = True
                .Open()
                fConectionString = .ConnectionString
                .Close()
            End With
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Return False
        End Try
    End Function
    Public Overloads Function CarregaConfig() As Boolean
        fConectionString = pClsPDVINT.ConnectionString
        fBanco.ConnectionString = fConectionString
        Return True
    End Function
    Public Function Conecta(Optional ByVal parExibeErro As Boolean = True, Optional ByVal parTentaConectar As Byte = 0) As Boolean
        Try
            If fBanco.State = 0 Then
                If fConectionString = "" Then
                    CarregaConfig()
                Else
                    fBanco.ConnectionString = fConectionString
                End If
                fBanco.CursorLocation = ADODB.CursorLocationEnum.adUseClient
                fBanco.CommandTimeout = 150000
                fBanco.Open()
            End If
            Conecta = True
        Catch ex As Exception
            If Err.Description.ToString = "ERRO NÃO ESPECIFICADO" And parTentaConectar < 30 Then
                CarregaConfig()
                parTentaConectar += 1
                Return Conecta(parExibeErro, parTentaConectar)
            End If
            GravaErro(Err, ex, , parExibeErro)
            Conecta = False
        End Try
    End Function
    Public Sub Desconecta()
        If fBanco.State = 1 Then fBanco.Close()
    End Sub
    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno As Decimal, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        PegarCampo = False
        If Not parConecta OrElse Conecta() Then
            With sDyna
                .Open(parSQL, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
                If Not .EOF Then
                    If Not IsDBNull(.Fields(0).Value) Then
                        parRetorno = .Fields(0).Value
                        PegarCampo = True
                    Else
                        parRetorno = 0
                    End If
                End If
                .Close()
            End With
            sDyna = Nothing
            If parConecta Then Desconecta()
        End If
    End Function
    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno As String, ByVal parTamanho As Byte, Optional ByVal parConecta As Boolean = False, Optional ByVal parIncrementar As Boolean = False) As Boolean
        PegarCampo(parSQL, parRetorno, parConecta)
        If parIncrementar Then
            If parRetorno = "" Then parRetorno = "0"
            parRetorno = Int(Val(parRetorno.Trim)) + 1
        End If
        parRetorno = IIf(parRetorno.Length < parTamanho, parRetorno.PadLeft(parTamanho, "0"), parRetorno)
        Return True
    End Function
    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno As String, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        PegarCampo = False
        parRetorno = ""
        If fBanco.State = 1 Then parConecta = False
        If Not parConecta OrElse Conecta() Then
            With sDyna
                TrataSql(parSQL)
                .Open(parSQL, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
                If Not .EOF Then
                    If Not IsDBNull(.Fields(0).Value) Then
                        parRetorno = .Fields(0).Value
                        PegarCampo = True
                    End If
                End If
                .Close()
            End With
            sDyna = Nothing
            If parConecta Then Desconecta()
        End If
    End Function
    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno As Date, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        PegarCampo = False
        If Not parConecta OrElse Conecta() Then
            With sDyna
                .Open(parSQL, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
                If Not .EOF Then
                    If Not IsDBNull(.Fields(0).Value) AndAlso IsDate(.Fields(0).Value) Then parRetorno = .Fields(0).Value
                    PegarCampo = True
                End If
                .Close()
            End With
            sDyna = Nothing
            If parConecta Then Desconecta()
        End If
    End Function
    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno() As String, Optional ByVal parConecta As Boolean = False, Optional ByVal parLinhas As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset, sCont As Integer
        PegarCampo = False
        If Not parConecta OrElse Conecta() Then
            With sDyna
                .Open(parSQL, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
                If Not .EOF Then
                    If parLinhas Then
                        ReDim parRetorno(.RecordCount - 1)
                        While Not .EOF
                            If Not IsDBNull(.Fields(0).Value) Then parRetorno(.AbsolutePosition - 1) = .Fields(0).Value
                            .MoveNext()
                        End While
                    Else
                        ReDim parRetorno(.Fields.Count - 1)
                        For sCont = 0 To .Fields.Count - 1
                            If Not IsDBNull(.Fields(sCont).Value) Then parRetorno(sCont) = .Fields(sCont).Value
                        Next

                    End If
                    PegarCampo = True
                End If
                .Close()
            End With
            If parConecta Then Desconecta()
            sDyna = Nothing
        End If
    End Function
    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno() As Integer, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        PegarCampo = False
        If Not parConecta OrElse Conecta() Then
            With sDyna
                .Open(parSQL, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
                If Not .EOF Then
                    ReDim parRetorno(.RecordCount - 1)
                    While Not .EOF
                        If Not IsDBNull(.Fields(0).Value) Then parRetorno(.AbsolutePosition - 1) = .Fields(0).Value
                        .MoveNext()
                    End While
                    PegarCampo = True
                End If
                .Close()
            End With
            If parConecta Then Desconecta()
            sDyna = Nothing
        End If
    End Function

    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno As Integer, Optional ByVal parIncrementa As Boolean = False, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        PegarCampo = False
        Try
            If Not parConecta OrElse Conecta() Then
                With sDyna
                    .Open(parSQL, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
                    If Not .EOF Then
                        If Not IsDBNull(.Fields(0).Value) AndAlso IsNumeric(.Fields(0).Value) Then parRetorno = .Fields(0).Value
                        If parIncrementa Then parRetorno = parRetorno + 1
                        PegarCampo = True
                    End If
                    .Close()
                End With
            End If
        Catch ex As Exception
            GravaErro(Err, ex)
        Finally
            sDyna = Nothing
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Overloads Function PegarCampo(ByVal parSQL As String, ByRef parRetorno As Byte, Optional ByVal parIncrementa As Boolean = False, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        PegarCampo = False
        Try
            If Not parConecta OrElse Conecta() Then
                With sDyna
                    .Open(parSQL, fBanco, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
                    If Not .EOF Then
                        If Not IsDBNull(.Fields(0).Value) Then parRetorno = .Fields(0).Value
                        If parIncrementa Then parRetorno = parRetorno + 1
                        PegarCampo = True
                    End If
                    .Close()
                End With
            End If
        Catch ex As Exception
            GravaErro(Err, ex)
        Finally
            sDyna = Nothing
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As TextBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If parCriptografa Then
                If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse Descriptografar(parDyna(parCampo).Value.ToString.Trim) <> parObjeto.Text.Trim Then
                    parDyna(parCampo).Value = Criptografar(parObjeto.Text.Trim)
                    fAlterou = True
                End If
                Return True
            Else
                If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value.ToString.Trim <> parObjeto.Text.Trim Then

                    parDyna(parCampo).Value = parObjeto.Text.Trim
                    fAlterou = True
                End If
                Return True
            End If
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Date, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value.ToString.Trim <> parObjeto Then
                If parObjeto = "00:00:00" Then
                    parDyna(parCampo).Value = DBNull.Value
                Else
                    parDyna(parCampo).Value = parObjeto
                End If
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As String, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If parCriptografa Then
                If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse Descriptografar(parDyna(parCampo).Value.ToString.Trim) <> parObjeto.Trim Then
                    parDyna(parCampo).Value = Criptografar(parObjeto.Trim)
                    fAlterou = True
                End If
                Return True
            Else
                If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value.ToString.Trim <> parObjeto.Trim Then
                    If parDyna(parCampo).DefinedSize < parObjeto.Trim.Length Then
                        parDyna(parCampo).Value = parObjeto.Trim.Substring(0, parDyna(parCampo).DefinedSize)
                    Else
                        parDyna(parCampo).Value = parObjeto.Trim
                    End If
                    fAlterou = True
                End If
                Return True
            End If
        Catch ex As Exception
            GravaErro(Err, ex, "Campo: " & parCampo & vbCrLf & "Valor: " & parObjeto.Trim)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As ComboBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If parDyna(parCampo).Type = ADODB.DataTypeEnum.adVarChar AndAlso parDyna(parCampo).DefinedSize > 10 Then
                If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto.Text Then
                    parDyna(parCampo).Value = parObjeto.Text
                    fAlterou = True
                End If
            Else
                If parObjeto.SelectedIndex = -1 Then
                    If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto.SelectedIndex Then
                        parDyna(parCampo).Value = 0
                        fAlterou = True
                    End If
                Else
                    If parObjeto.DataSource Is Nothing Then
                        If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto.SelectedIndex Then
                            parDyna(parCampo).Value = parObjeto.SelectedIndex
                            fAlterou = True
                        End If
                    Else
                        If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto.SelectedValue Then
                            parDyna(parCampo).Value = parObjeto.SelectedValue
                            fAlterou = True
                        End If
                    End If
                End If
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As MaskedTextBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value.ToString.Trim <> parObjeto.Text.Trim Then
                parDyna(parCampo).Value = parObjeto.Text.Trim
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As NumericUpDown, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto.Value Then
                parDyna(parCampo).Value = parObjeto.Value
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto() As Byte, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            'If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value.ToString <> parObjeto.ToString Then
            parDyna(parCampo).Value = parObjeto
            fAlterou = True
            'End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As DateTimePicker, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto.Text Then
                parDyna(parCampo).Value = parObjeto.Text
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As CheckBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value Is Nothing OrElse parDyna(parCampo).Value <> IIf(parObjeto.Checked, 1, 0) Then
                parDyna(parCampo).Value = IIf(parObjeto.Checked, 1, 0)
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Integer, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto Then
                parDyna(parCampo).Value = parObjeto
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Long, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto Then
                parDyna(parCampo).Value = parObjeto
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Byte, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto Then
                parDyna(parCampo).Value = parObjeto
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Boolean, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Dim sRes As Byte
        sRes = IIf(parObjeto, 1, 0)
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> sRes Then
                parDyna(parCampo).Value = sRes
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As GroupBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Dim sRes As String = ""
        For Each sObjeto As RadioButton In parObjeto.Controls
            If sObjeto.Checked Then
                sRes = sObjeto.Tag
            End If
        Next
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> sRes Then
                parDyna(parCampo).Value = sRes
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Double, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto Then
                parDyna(parCampo).Value = parObjeto
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Decimal, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto Then
                parDyna(parCampo).Value = parObjeto
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Function Passa(ByRef parObjeto As Button, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, ByVal parCriptografa As Boolean) As Boolean
        Try
            If IsDBNull(parDyna(parCampo).Value) OrElse parDyna(parCampo).Value = Nothing OrElse parDyna(parCampo).Value <> parObjeto.BackColor.ToKnownColor Then
                'parDyna(parCampo).Value = parObjeto.BackColor.ToKnownColor
                parDyna(parCampo).Value = parObjeto.BackColor.ToArgb
                fAlterou = True
            End If
            Return True
        Catch ex As Exception
            GravaErro(Err, ex)
            Passa = False
        End Try
    End Function
    Public Overloads Sub Recebe(ByRef parObjeto() As Byte, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto = parDyna(parCampo).Value
        Else
            parObjeto = Nothing
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As String, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            If parDescriptografar Then
                parObjeto = Descriptografar(parDyna(parCampo).Value.ToString.Trim)
            ElseIf parSemTrim Then
                parObjeto = parDyna(parCampo).Value
            Else
                parObjeto = parDyna(parCampo).Value.ToString.Trim
            End If
        Else
            parObjeto = ""
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As Date, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            If parDescriptografar Then
                parObjeto = Descriptografar(parDyna(parCampo).Value.ToString.Trim)
            Else
                parObjeto = parDyna(parCampo).Value.ToString.Trim
            End If
        Else
            parObjeto = Nothing
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As Integer, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto = Val(parDyna(parCampo).Value)
        Else
            parObjeto = 0
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As Long, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto = Val(parDyna(parCampo).Value)
        Else
            parObjeto = 0
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As Decimal, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) AndAlso parDyna(parCampo).Value <> Nothing Then
            parObjeto = parDyna(parCampo).Value
        Else
            parObjeto = 0
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As Byte, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            If parDyna(parCampo).Value = True Then
                parObjeto = 1
            ElseIf parDyna(parCampo).Value = False Then
                parObjeto = 0
            Else
                parObjeto = parDyna(parCampo).Value.ToString.Trim
            End If

        Else
            parObjeto = 0
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As GroupBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False, Optional ByVal parSemTrim As Boolean = False)
        Dim sRes As String = ""
        If Not IsDBNull(parDyna(parCampo).Value) Then
            sRes = parDyna(parCampo).Value.ToString.Trim
        Else
            sRes = "NULL"
        End If
        For Each sObjeto As RadioButton In parObjeto.Controls
            If sObjeto.Tag = sRes Then
                sObjeto.Checked = True
            Else
                sObjeto.Checked = False
            End If
        Next
    End Sub
    Public Overloads Function Recebe(ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parTamanho As Integer = 0) As String
        Dim sTexto As String
        If IsDBNull(parDyna.Fields(parCampo).Value) Then
            sTexto = ""
        Else
            sTexto = parDyna.Fields(parCampo).Value
        End If
        If parTamanho > 0 AndAlso Len(sTexto) > parTamanho Then sTexto = sTexto.Substring(0, parTamanho)
        Return sTexto
    End Function

    Public Overloads Sub Recebe(ByRef parObjeto As Boolean, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto = IIf(parDyna(parCampo).Value = 1, True, False)
        Else
            parObjeto = False
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As DateTimePicker, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto.Text = parDyna(parCampo).Value
        Else
            parObjeto.Text = ""
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As Button, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            'parObjeto.BackColor = System.Drawing.Color.FromKnownColor(parDyna(parCampo).Value)
            parObjeto.BackColor = System.Drawing.Color.FromArgb(parDyna(parCampo).Value)
        Else
            parObjeto.BackColor = Color.Silver
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As TextBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            If parDescriptografar Then
                parObjeto.Text = Descriptografar(parDyna(parCampo).Value.ToString.Trim)
            Else
                parObjeto.Text = parDyna(parCampo).Value.ToString.Trim
            End If
        Else
            parObjeto.Text = ""
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As ComboBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        If parDyna(parCampo).Type = ADODB.DataTypeEnum.adVarChar AndAlso parDyna(parCampo).DefinedSize > 8 Then
            If Not IsDBNull(parDyna(parCampo).Value) Then
                For sCont As Integer = 0 To parObjeto.Items.Count - 1
                    If parObjeto.Items(sCont).ToString = parDyna(parCampo).Value Then
                        parObjeto.SelectedIndex = sCont
                        Exit For
                    End If
                    If parObjeto.SelectedIndex <> sCont Then
                        parObjeto.Text = parDyna(parCampo).Value
                    End If
                Next
            Else
                parObjeto.SelectedIndex = -1
            End If
        Else
            If parObjeto.Items.Count > 0 Then
                If parObjeto.DataSource Is Nothing Then
                    If Not IsDBNull(parDyna(parCampo).Value) Then
                        parObjeto.SelectedIndex = parDyna(parCampo).Value
                    Else
                        parObjeto.SelectedIndex = 0
                    End If
                Else
                    If Not IsDBNull(parDyna(parCampo).Value) Then
                        parObjeto.SelectedValue = parDyna(parCampo).Value
                    Else
                        parObjeto.SelectedValue = 0
                    End If
                End If
            End If
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As MaskedTextBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto.Text = parDyna(parCampo).Value.ToString.Trim
        Else
            parObjeto.Text = ""
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As NumericUpDown, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto.Value = parDyna(parCampo).Value
        Else
            parObjeto.Value = 0
        End If
    End Sub
    Public Overloads Sub Recebe(ByRef parObjeto As CheckBox, ByVal parDyna As ADODB.Recordset, ByVal parCampo As String, Optional ByVal parDescriptografar As Boolean = False)
        Dim sVisivel As Boolean
        sVisivel = parObjeto.Visible
        If sVisivel Then parObjeto.Visible = False
        If Not IsDBNull(parDyna(parCampo).Value) Then
            parObjeto.Checked = IIf(parDyna(parCampo).Value = 1, True, False)
        Else
            parObjeto.Checked = False
        End If
        If sVisivel Then parObjeto.Visible = sVisivel
    End Sub
    Public Overloads Function Plic(ByVal parCampo As String, ByVal parComparacao As enuTComparacao, ByVal parConcatena As enuTConcatena, Optional ByVal parObjeto As Object = Nothing) As String
        Plic = parCampo
        Select Case parConcatena
            Case enuTConcatena.Where
                Plic = " WHERE " & Plic
            Case enuTConcatena.EAnd
                Plic = " AND " & Plic
            Case enuTConcatena.EOr
                Plic = " OR " & Plic
            Case eAP.eBET
                Plic = " BETWEEN " & Plic
        End Select
        Select Case parComparacao
            Case enuTComparacao.Igual
                Plic = Plic & " = "
            Case enuTComparacao.Diferente
                Plic = Plic & " <> "
        End Select
        Plic = Plic & Plic(parObjeto)
    End Function
    Public Overloads Function Plic(ByVal parCampo As Date) As String
        If IsDate(parCampo) AndAlso parCampo.Year > 1 AndAlso parCampo.Hour > 0 Or parCampo.Hour > 0 Then
            Plic = "'" & CType(parCampo, Date).Date & " " & CType(parCampo, Date).Hour & ":" & CType(parCampo, Date).Minute & ":" & CType(parCampo, Date).Second & "'"
        ElseIf IsDate(parCampo) AndAlso parCampo.Year > 1 Then
            Plic = "'" & CType(parCampo, Date).Date & "'"
        Else
            Plic = "NULL"
        End If
    End Function
    Public Overloads Function Plic(ByVal parTexto As Object, ByVal parTipo As enuTPlic) As String
        Select Case parTipo
            Case enuTPlic.Values
                Plic = " VALUES ("
            Case Else
                Plic = ""
        End Select
        Plic = Plic & Plic(parTexto)
        Select Case parTipo
            Case enuTPlic.Parentes
                Plic = Plic & ")"
            Case enuTPlic.Virgula, enuTPlic.Values
                Plic = Plic & ","
        End Select
    End Function
    Public Overloads Function Plic(ByVal parTipo As enuTPlic) As String
        Select Case parTipo
            Case enuTPlic.Values
                Plic = " VALUES ("
            Case Else
                Plic = ""
        End Select
        Plic = Plic & "'" & Now & "'"
        Select Case parTipo
            Case enuTPlic.Parentes
                Plic = Plic & ")"
            Case enuTPlic.Virgula, enuTPlic.Values
                Plic = Plic & ","
        End Select
    End Function
    Public Overloads Function Plic(ByVal parTexto As TextBox, Optional ByVal parTipo As enuTPlic = enuTPlic.Nenhum) As String
        Select Case parTipo
            Case enuTPlic.Values
                Plic = " VALUES ("
            Case Else
                Plic = ""
        End Select
        Plic = Plic & "'" & parTexto.Text.Trim & "'"
        Select Case parTipo
            Case enuTPlic.Parentes
                Plic = Plic & ")"
            Case enuTPlic.Virgula, enuTPlic.Values
                Plic = Plic & ","
        End Select
    End Function
    Public Overloads Function Plic(ByVal parTexto As NumericUpDown, Optional ByVal parTipo As enuTPlic = enuTPlic.Nenhum) As String
        Select Case parTipo
            Case enuTPlic.Values
                Plic = " VALUES ("
            Case Else
                Plic = ""
        End Select
        Plic = Plic & parTexto.Value
        Select Case parTipo
            Case enuTPlic.Parentes
                Plic = Plic & ")"
            Case enuTPlic.Virgula, enuTPlic.Values
                Plic = Plic & ","
        End Select
    End Function
    Public Overloads Function Plic(ByVal parTexto As String, Optional ByVal parTipo As enuTPlic = enuTPlic.Nenhum) As String
        Select Case parTipo
            Case enuTPlic.Values
                Plic = " VALUES ("
            Case Else
                Plic = ""
        End Select
        parTexto = parTexto.Trim.Replace("'", "' + '''' + '")
        Plic = Plic & "'" & parTexto & "'"
        Select Case parTipo
            Case enuTPlic.Parentes
                Plic = Plic & ")"
            Case enuTPlic.Virgula, enuTPlic.Values
                Plic = Plic & ","
        End Select
    End Function
    Public Overloads Function Plic(ByVal parInt As Integer, Optional ByVal parTipo As enuTPlic = enuTPlic.Nenhum) As String
        Select Case parTipo
            Case enuTPlic.Values
                Plic = " VALUES ("
            Case Else
                Plic = ""
        End Select
        Plic = Plic & parInt
        Select Case parTipo
            Case enuTPlic.Parentes
                Plic = Plic & ")"
            Case enuTPlic.Virgula, enuTPlic.Values
                Plic = Plic & ","
        End Select
    End Function
    Public Overloads Function Plic(ByVal parDouble As Double) As String
        Plic = parDouble.ToString.Replace(",", ".")
    End Function
    Public Overloads Function Plic(ByVal parDouble As Decimal) As String
        Plic = parDouble.ToString.Replace(",", ".")
    End Function
    Public Overloads Function Plic(ByVal parCombo As ComboBox) As String
        If parCombo.SelectedValue.ToString.Length >= 7 Then
            Plic = Plic(parCombo.SelectedValue.ToString)
        Else
            Plic = parCombo.SelectedValue
        End If
    End Function
    Public Overloads Function Plic(ByVal sCheck As CheckBox) As Integer
        If sCheck.Checked = True Then
            Plic = 1
        Else
            Plic = 0
        End If
    End Function
    Public Overloads Function Plic(ByVal sGrid As DataGridView, ByVal sCol As Integer, Optional ByVal sRow As Integer = 0) As String
        With sGrid
            If sRow > 0 Then
                Plic = .Rows(sRow).Cells.Item(sCol).Value
            Else
                Plic = .CurrentRow.Cells.Item(sCol).Value
            End If
        End With
    End Function


    Public Overloads Function CarregaObjeto(ByRef parListaUsuario As List(Of clsUsuario), ByVal parTabela As String, ByVal parCondicao As String, ByVal parConecta As Boolean) As Boolean
        Dim sDyna As New ADODB.Recordset
        Dim sSql As String = ""
        Try
            fProcessando = True
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT * "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql = sSql & "FROM "
                End If
                sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sDyna.Open(sSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")

                While Not sDyna.EOF
                    Dim user = New clsUsuario
                    user.Matricula = sDyna.Fields("US_MATRICULA").Name
                    parListaUsuario.Add(user)
                    sDyna.MoveNext()
                End While

                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function


    Public Overloads Function CarregaObjeto(ByRef parGrafico As DevExpress.XtraCharts.ChartControl, ByVal parHorizontal As String, ByVal parVertical As String, ByVal parValor As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        Dim sSql As String = ""
        sSql = "SELECT " & parHorizontal & "," & parVertical & " AS CAMPO " & ", " & parValor & " AS VALOR "
        If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
            sSql = sSql & "FROM "
        End If
        sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
        Return CarregaObjeto(parGrafico, sSql, parConecta)
    End Function
    Public Overloads Function CarregaObjeto(ByRef parGrafico As DevExpress.XtraCharts.ChartControl, ByVal parSql As String, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sDyna.Open(parSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")
                With parGrafico
                    .Series.Clear()
                    .SeriesDataMember = sDyna.Fields(0).Name
                    .SeriesTemplate.ArgumentDataMember = sDyna.Fields(1).Name
                    .SeriesTemplate.ValueDataMembers.AddRange(sDyna.Fields(2).Name)
                    .DataSource = ds.Tables("TABELA")
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, parSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parCombo As ComboBox, ByVal parCodigo As String, ByVal parCampo As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        Dim sSql As String = ""
        Try
            fProcessando = True
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT " & parCodigo & ",RTRIM(LTRIM(" & parCampo & ")) AS CAMPO "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql = sSql & "FROM "
                End If
                sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sDyna.Open(sSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")
                With parCombo
                    .ValueMember = sDyna.Fields(0).Name
                    .DisplayMember = sDyna.Fields(1).Name
                    .DataSource = ds.Tables("TABELA")
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parDados As DataSet, Optional ByVal parConecta As Boolean = False, Optional ByVal parSQL As String = "") As Boolean
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            If Not parConecta OrElse Conecta() Then
                sDyna.Open(parSQL, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                parDados = New DataSet
                da.Fill(parDados, sDyna, "TABELA")
                da = Nothing
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, parSQL)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parGrid As DevExpress.XtraPivotGrid.PivotGridControl, ByVal parDados As DataSet) As Boolean
        Try
            With parGrid
                .DataSource = Nothing
                .DataSource = parDados.Tables("TABELA")
                .Refresh()
            End With
            Return True
        Catch ex As Exception
            GravaErro(Err, ex, "")
            Return False
        Finally
            fProcessando = False
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parGrid As DevExpress.XtraPivotGrid.PivotGridControl, Optional ByVal parConecta As Boolean = False, Optional ByVal parSQL As String = "") As Boolean
        Dim sData As New DataSet
        Try
            If CarregaObjeto(sData, parConecta, parSQL) Then
                Return CarregaObjeto(parGrid, sData)
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, parSQL)
            Return False
        Finally
            fProcessando = False
            sData = Nothing
        End Try
    End Function
    'Public Overloads Function CarregaObjeto(ByRef parGrid As DevExpress.XtraTreeList.TreeList, Optional ByVal parConecta As Boolean = False, Optional ByVal parSQL As String = "") As Boolean
    '    Dim sDyna As New ADODB.Recordset
    '    Try
    '        fProcessando = True
    '        If Not parConecta OrElse Conecta() Then
    '            sDyna.Open(parSQL, fBanco, 1, 2) '16

    '            Dim da As New OleDbDataAdapter
    '            Dim ds As New DataSet

    '            da.Fill(ds, sDyna, "TABELA")
    '            With parGrid

    '                .DataSource = ds.Tables("TABELA")
    '                .Refresh()
    '                ds = Nothing
    '                da = Nothing
    '            End With
    '            Return True
    '        Else
    '            Return False
    '        End If
    '    Catch ex As Exception
    '        GravaErro(Err, ex, parSQL)
    '        Return False
    '    Finally
    '        fProcessando = False
    '        Libera(sDyna)
    '        If parConecta Then Desconecta()

    '    End Try
    'End Function
    Public Overloads Function CarregaObjeto(ByRef parGrid As DevExpress.XtraGrid.GridControl, Optional ByVal parConecta As Boolean = False, Optional ByVal parSQL As String = "") As Boolean
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            If Not parConecta OrElse Conecta() Then
                sDyna.Open(parSQL, fBanco, 1, 2) '16
                'While sDyna.State <> 0
                '    Application.DoEvents()
                'End While


                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet

                da.Fill(ds, sDyna, "TABELA")
                With parGrid

                    .DataSource = ds.Tables("TABELA")
                    .Refresh()
                    ds = Nothing
                    da = Nothing
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, parSQL)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()

        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parCombo As DevExpress.XtraEditors.LookUpEdit, ByVal parCodigo As String, ByVal parCampo As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sSql As String = ""
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            Cursor.Current = Cursors.WaitCursor
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT DISTINCT " & parCodigo & ",RTRIM(LTRIM(" & parCampo & ")) AS CAMPO "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql = sSql & "FROM "
                End If
                sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sSql &= " ORDER BY CAMPO"
                sDyna.Open(sSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")
                With parCombo
                    .Properties.BeginUpdate()
                    .Properties.ValueMember = sDyna.Fields(0).Name
                    .Properties.DisplayMember = sDyna.Fields(1).Name
                    .Properties.DataSource = ds.Tables("TABELA")
                    .Properties.EndUpdate()
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
            Cursor.Current = Cursors.Default
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parCombo As DevExpress.XtraEditors.CheckedComboBoxEdit, ByVal parCodigo As String, ByVal parCampo As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sSql As String = ""
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            Cursor.Current = Cursors.WaitCursor
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT DISTINCT " & parCodigo & ",RTRIM(LTRIM(" & parCampo & ")) AS CAMPO "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql = sSql & "FROM "
                End If
                sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sSql &= " ORDER BY CAMPO"
                sDyna.Open(sSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")
                With parCombo
                    .Properties.BeginUpdate()
                    .Properties.ValueMember = sDyna.Fields(0).Name
                    .Properties.DisplayMember = sDyna.Fields(1).Name
                    .Properties.DataSource = ds.Tables("TABELA")
                    .Properties.EndUpdate()
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
            Cursor.Current = Cursors.Default
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parCombo As DevExpress.XtraEditors.Repository.RepositoryItemCheckedComboBoxEdit, ByVal parCodigo As String, ByVal parCampo As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sSql As String = ""
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            Cursor.Current = Cursors.WaitCursor
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT DISTINCT " & parCodigo & ",RTRIM(LTRIM(" & parCampo & ")) AS CAMPO "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql = sSql & "FROM "
                End If
                sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sSql &= " ORDER BY CAMPO"
                sDyna.Open(sSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")
                With parCombo
                    .BeginUpdate()
                    .ValueMember = sDyna.Fields(0).Name
                    .DisplayMember = sDyna.Fields(1).Name
                    .DataSource = ds.Tables("TABELA")
                    .EndUpdate()
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
            Cursor.Current = Cursors.Default
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parCombo As DevExpress.XtraEditors.Repository.RepositoryItemComboBox, ByVal parCodigo As String, ByVal parCampo As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sSql As String = ""
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            Cursor.Current = Cursors.WaitCursor
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT DISTINCT " & parCodigo & ",RTRIM(LTRIM(" & parCampo & ")) AS CAMPO "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql = sSql & "FROM "
                End If
                sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sSql &= " ORDER BY CAMPO"
                sDyna.Open(sSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")
                With parCombo
                    .BeginUpdate()
                    '.Items
                    '.EditValueChangedDel = sDyna.Fields(0).Name
                    '.DisplayMember = sDyna.Fields(1).Name
                    '.DataSource = ds.Tables("TABELA")
                    '.EndUpdate()
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
            Cursor.Current = Cursors.Default
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parCombo As DevExpress.XtraBars.BarEditItem, ByVal parCodigo As String, ByVal parCampo As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sSql As String = ""
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            Cursor.Current = Cursors.WaitCursor
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT DISTINCT " & parCodigo & ",RTRIM(LTRIM(" & parCampo & ")) AS CAMPO "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql = sSql & "FROM "
                End If
                sSql = sSql & parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sSql &= " ORDER BY CAMPO"
                sDyna.Open(sSql, fBanco, 1, 2)
                While Not sDyna.EOF
                    CType(parCombo.Edit, DevExpress.XtraEditors.Repository.RepositoryItemCheckedComboBoxEdit).Items.Add(New CheckedListBoxItem(sDyna.Fields(0).Value, CStr(sDyna.Fields(1).Value)))
                    sDyna.MoveNext()
                End While
                CType(parCombo.Edit, DevExpress.XtraEditors.Repository.RepositoryItemCheckedComboBoxEdit).ForceUpdateEditValue = DevExpress.Utils.DefaultBoolean.True
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
            Cursor.Current = Cursors.Default
        End Try
    End Function
    Public Shared Sub PreparaDataTable_AddColuna(ByRef parTabela As DataTable, ByVal parCampo As String, ByVal parTipo As Type)
        parTabela.Columns.Add((New DataColumn(parCampo, parTipo)))
    End Sub
    Public Shared Function PreparaDataTable(ByRef parGrid As DevExpress.XtraGrid.Views.Grid.GridView) As DataTable
        Dim sTable As New DataTable()
        For Each sCol As DevExpress.XtraGrid.Columns.GridColumn In parGrid.Columns
            Dim sType As Type
            Select Case sCol.DisplayFormat.FormatType
                Case DevExpress.Utils.FormatType.Numeric
                    sType = GetType(Integer)
                Case DevExpress.Utils.FormatType.DateTime
                    sType = GetType(Date)
                Case Else
                    sType = GetType(String)
            End Select
            sTable.Columns.Add((New DataColumn(sCol.FieldName, sType)))
        Next
        Return sTable
    End Function
    Public Overloads Function CarregaObjeto(ByRef parTable As DataTable, ByVal parSql As String, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sDyna.Open(parSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                da.Fill(parTable, sDyna)
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, parSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parGrid As Array, ByVal parSql As String, Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            If Not parConecta OrElse Conecta() Then
                sDyna.Open(parSql, fBanco, 1, 2)
                parGrid = sDyna.GetRows()
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, parSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Overloads Function CarregaObjeto(ByRef parGrid As DataGridViewComboBoxColumn, ByVal parCodigo As String, ByVal parCampo As String, ByVal parTabela As String, Optional ByVal parCondicao As String = "", Optional ByVal parConecta As Boolean = False) As Boolean
        Dim sSql As String = ""
        Dim sDyna As New ADODB.Recordset
        Try
            fProcessando = True
            If fBanco.State = 1 Then parConecta = False
            If Not parConecta OrElse Conecta() Then
                sSql = "SELECT " & parCodigo & ",LTRIM(" & parCampo & ") AS CAMPO "
                If Not LTrim(parTabela).Substring(0, 4).ToUpper = "FROM" Then
                    sSql &= "FROM "
                End If
                sSql &= parTabela & IIf(parCondicao = "", "", " WHERE " & parCondicao)
                sSql &= " ORDER BY " & parCampo
                sDyna.Open(sSql, fBanco, 1, 2)
                Dim da As New OleDbDataAdapter
                Dim ds As New DataSet
                da.Fill(ds, sDyna, "TABELA")
                With parGrid
                    .ValueMember = sDyna.Fields(0).Name
                    .DisplayMember = sDyna.Fields(1).Name
                    .DataSource = ds.Tables("TABELA")
                End With
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function
    Public Function Registra(ByVal ParTipo As enuTipoRegistro, ByVal parCodFilial As String, ByRef parCodigo As Object, Optional ByVal parTamanho As Byte = 0, Optional ByVal parIncluirFilial As Boolean = False) As Boolean
        Dim sSql As String = "", sCodigo As String = ""
        Try
            sSql = "SELECT REG_ULTIMO FROM REGISTROS WHERE REG_TIPO =" & Plic(CInt(ParTipo))
            PegarCampo(sSql, sCodigo, True)
            If sCodigo = "" Then
                Select Case ParTipo
                    Case enuTipoRegistro.eRegSeparacao
                        sSql = "SELECT MAX(SEP_CODIGO) FROM SEPARACAOPEDIDO"
                        PegarCampo(sSql, sCodigo, True)
                    Case enuTipoRegistro.eRegPedido_Compra
                        sSql = "SELECT MAX(PED_PEDIDO) FROM PEDIDOS"
                        PegarCampo(sSql, sCodigo, True)
                    Case enuTipoRegistro.eRegSeparacao
                        sSql = "SELECT MAX(REM_CODIGO) FROM REMANEJAMENTO WHERE MID(REM_CODIGO,1,4) =" & Plic(parCodFilial.PadLeft(4, "0"))
                        PegarCampo(sSql, sCodigo, True)
                        If sCodigo.Length > 4 Then sCodigo = Val(Mid(sCodigo, 5))
                End Select
                sCodigo = Val(sCodigo)
                sSql = "INSERT INTO REGISTROS(REG_TIPO , REG_ULTIMO) VALUES(" & Plic(ParTipo, enuTPlic.Virgula) & Plic(sCodigo, enuTPlic.Parentes)
                If Not ExecutaSql(sSql) Then Return False
            End If
            sCodigo = sCodigo + 1
            sSql = "UPDATE REGISTROS SET REG_ULTIMO =" & Plic(sCodigo) & " WHERE REG_TIPO = " & Plic(CInt(ParTipo))
            If Not ExecutaSql(sSql) Then Return False
            If parTamanho > 0 Then parCodigo = sCodigo.PadLeft(parTamanho, "0")
            If parIncluirFilial Then parCodigo = parCodFilial.PadLeft(4, "0") & parCodigo
            Return True
        Catch ex As Exception
            GravaErro(Err, ex, sSql)
            Return False
        End Try
    End Function
    Private Function Executa(ByRef parSQL As String, ByVal parConecta As Boolean, ByVal parAssincrono As Boolean) As Boolean
        Dim sComando As ADODB.Command
        If fBanco.State = 1 Then parConecta = False
        If Not parConecta OrElse Conecta() Then
            If fBanco.State = 1 Then
                TrataSql(parSQL)
                If (InStr(1, parSQL, "UPDATE") > 0 Or InStr(1, parSQL, "DELETE") > 0) Or (parAssincrono) Then
                    parSQL = Replace(parSQL, ".*", " ")
                    sComando = New ADODB.Command
                    sComando.Prepared = False
                    sComando.ActiveConnection = fBanco
                    sComando.CommandType = ADODB.CommandTypeEnum.adCmdText
                    sComando.CommandText = parSQL
                    sComando.CommandTimeout = 300
                    'If parAssincrono Then
                    sComando.Execute(, , ADODB.ExecuteOptionEnum.adAsyncExecute)
                    While sComando.State <> 0
                        Application.DoEvents()
                    End While
                    'Else
                    '    sComando.Execute()
                    'End If
                Else
                    fBanco.Execute(parSQL, 128) ', ADODB.ExecuteOptionEnum.adAsyncExecute
                End If
                GravaPDVLOG(parSQL)
                sComando = Nothing
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function
    Public Function ExecutaSql(ByRef parSQL As String, Optional ByVal parConecta As Boolean = False, Optional ByVal parAssincrono As Boolean = False, Optional ByVal parSem_Tratamento_Erro As Boolean = False) As Boolean
        If parSQL = "" Then Return True
        If parSem_Tratamento_Erro Then
            Return Executa(parSQL, parConecta, parAssincrono)
        Else
            Try
                Return Executa(parSQL, parConecta, parAssincrono)
            Catch ex As Exception
                GravaErro(Err, ex, parSQL)
                Return False
            Finally
                If parConecta Then Desconecta()
            End Try
        End If
    End Function
    Private Sub GravaPDVLOG(ByVal parSql As String)
        Dim sContaErro As Byte = 0
        On Error GoTo Trata
        IO.File.AppendAllText(pDir & "AUDIT\NET" + Format(Now.Year, "0000") + Format(Now.Month, "00") + ".TXT", Now & "|" & parSql & vbCrLf)
        Exit Sub
Trata:
        If sContaErro < 30 Then 'O sistema vai tentar gerar o pdvlog durante 15 segundos
            System.Threading.Thread.Sleep(500)
            sContaErro += 1
            Resume
        End If
        GravaErro(Err)
    End Sub
    Public Function TrataSql(ByRef fSql As String) As Boolean
        'não pode ter UCASE POR CAUSA DA ENCRIPTAÇÃO
        'fSql = UCase(fSql)
        Dim sSql As String, sConta As Integer, sConta2 As Integer, sSobra As String
        If InStr(1, UCase(fSql), "MID(") > 0 Then
            fSql = Replace(fSql, "MID(", "SUBSTRING(")
        End If
        If InStr(1, fSql, "DATE()") > 1 Then
            fSql = Replace(fSql, "DATE()", "GETDATE()")
        End If
        If InStr(1, UCase(fSql), "NOW(") > 0 Then
            fSql = Replace(fSql, "NOW(", "GETDATE(")
        End If
        If InStr(1, UCase(fSql), "DATEDIFF('YYYY'") > 0 Then
            fSql = Replace(fSql, "DATEDIFF('YYYY'", "DATEDIFF(YEAR")
        End If
        If InStr(1, UCase(fSql), "TRIM(") > 0 And InStr(1, UCase(fSql), "RTRIM(") = 0 And InStr(1, UCase(fSql), "LTRIM(") = 0 Then
            fSql = Replace(fSql, "TRIM(", "RTRIM(")
            fSql = Replace(fSql, "trim(", "RTRIM(")
            fSql = Replace(fSql, "Ttrim(", "RTRIM(")
        End If
        If InStr(1, UCase(fSql), "UCASE(") > 0 Then
            fSql = Replace(fSql, "UCASE(", "UPPER(")
        End If
        If InStr(1, UCase(fSql), "ISNUMERIC(") > 0 Then
            sConta = InStr(1, UCase(fSql), "ISNUMERIC(")
            sConta = InStr(sConta, fSql, ")")
            sSobra = fSql.Substring(sConta, Len(fSql))

            sSql = fSql.Substring(0, sConta) + "<> 0 "
            fSql = sSql + sSobra
        End If
        If InStr(1, UCase(fSql), "ISDATE(") > 0 Then
            sConta = InStr(1, UCase(fSql), "ISDATE(")
            sConta = InStr(sConta, fSql, ")")
            sSql = fSql.Substring(0, sConta) + "= 1" & fSql.Substring(sConta)
            fSql = sSql
        End If
        If InStr(1, UCase(fSql), "WEEKDAY(") > 0 Then
            fSql = Replace(fSql, "WEEKDAY(", "DATEPART(day,")
        End If
        If InStr(1, UCase(fSql), "DISTINCTROW") > 0 Then
            fSql = Replace(fSql, "DISTINCTROW", "DISTINCT")
        End If
        While InStr(1, UCase(fSql), "ISNULL(") > 0
            sConta = InStr(1, UCase(fSql), "ISNULL(")
            sSql = fSql.Substring(0, sConta - 1)
            fSql = fSql.Substring(0, sConta + 7)
            sConta = InStr(1, UCase(fSql), ")")
            fSql = sSql & fSql.Substring(0, sConta - 1) & " IS NULL " & fSql.Substring(sConta)
            'ALTERADO 11/03/2009 CARTÃO MERCADO
            'fSql = sSql & Mid(fSql, 1, sConta - 1) & " =  NULL " & Mid(fSql, sConta + 1)
        End While

        If pClsPDVINT.TipoBanco = 1 Then
            If InStr(1, fSql, "UPDATE") = 1 Then
                sConta = InStr(1, UCase(fSql), "SET")
                sSql = "FROM " & fSql.Substring(7, sConta - 8)
                If InStr(1, sSql, ",") > 0 Then
                    sConta2 = InStr(1, UCase(fSql), ",")
                    fSql = fSql.Substring(0, sConta2 - 1) & fSql.Substring(sConta - 2)
                    sConta = InStr(1, UCase(fSql), "WHERE")
                    fSql = fSql.Substring(0, sConta - 1) & sSql & fSql.Substring(sConta - 1)
                End If
            End If
        End If
        fSql = TrataIIF(fSql)
        Return True
    End Function
    Public Function TrataIIF(ByVal pSql As String, Optional ByVal pPos As Byte = 0) As String
        Dim sConta As Integer, sSql As String
        If pPos = 0 Then pPos = 1
        TrataIIF = ""
        sConta = InStr(1, UCase(pSql), "IIF(")
        If sConta > 0 Then
            TrataIIF &= pSql.Substring(0, sConta - 1)
            pSql = pSql.Substring(sConta + 3)
            sConta = InStr(1, UCase(pSql), ",")
            sSql = pSql.Substring(0, sConta - 1)
            pSql = pSql.Substring(sConta)
            TrataIIF &= "TAB" & pPos & "= CASE WHEN " & verBoolean(sSql)
            sConta = InStr(1, UCase(pSql), ",")
            sSql = pSql.Substring(0, sConta - 1)
            pSql = pSql.Substring(sConta)
            TrataIIF = TrataIIF & " THEN " & sSql
            sConta = InStr(1, UCase(pSql), ")")
            sSql = pSql.Substring(0, sConta - 1)
            pSql = pSql.Substring(sConta)
            TrataIIF = TrataIIF & " ELSE " & sSql & " END "
        Else
            TrataIIF = pSql
            Exit Function
        End If
        sConta = InStr(1, Left(UCase(pSql), 5), "AS ")
        If sConta > 0 Then
            pSql = pSql.Substring(sConta)
            sConta = InStr(1, pSql, ",")
            pSql = pSql.Substring(0, sConta - 1)
            pSql = pSql.Substring(sConta - 1)
            TrataIIF = Replace(TrataIIF, "TAB" & pPos, pSql)
        End If
        TrataIIF = TrataIIF & TrataIIF(pSql, pPos + 1)
    End Function
    Public Function verBoolean(ByVal pSql As String) As String
        verBoolean = ""
        If InStr(1, pSql, "<>") > 0 Or InStr(1, pSql, "=") > 0 Or InStr(1, UCase(pSql), "NOT IN") Or InStr(1, UCase(pSql), ">") Then
            verBoolean = pSql
        Else
            If InStr(1, UCase(pSql), "ISNULL") > 0 Then
                If InStr(1, UCase(pSql), "NOT") > 0 Then verBoolean = "NOT "
                verBoolean = " " & verBoolean & pSql.Substring(0, InStr(1, pSql, ")") - 1) & ",'') = '' " & pSql.Substring(InStr(1, pSql, ")"))
            Else
                If InStr(1, UCase(pSql), "BETWEEN") > 0 Or InStr(1, UCase(pSql), "LIKE") > 0 Or InStr(1, UCase(pSql), "')") > 0 Then
                    verBoolean = " " & pSql
                ElseIf InStr(1, UCase(pSql), "NOT") > 0 Then
                    verBoolean = " " & pSql.Substring(InStr(1, UCase(pSql), "NOT") + 2) & " = 0 "
                Else
                    verBoolean = " " & pSql & " = 1 "
                End If
            End If
        End If
    End Function

    Public Overloads Function CarregaObjeto(ByRef parGrid As DataGridView, ByVal parFrom As String, ByVal parCondicao As String, Optional ByVal parConecta As Boolean = False, Optional ByVal parAutomatico As Boolean = True, Optional ByVal parSQL As String = "") As Boolean
        Dim sCont As Integer, sSQL As String
        Dim sDyna As New ADODB.Recordset, sCampos() As String
        Try
            fProcessando = True
            If Not parConecta OrElse Conecta() Then
                If parSQL = "" Then
                    With parGrid
                        sSQL = ""
                        ReDim sCampos(.ColumnCount)
                        For sCont = 0 To .ColumnCount - 1
                            If .Columns.Item(sCont).DataPropertyName = "EDITAR" Then
                                sSQL = sSQL & IIf(sSQL = "", "", ",") & "'Editar' as EDITAR"
                            ElseIf .Columns.Item(sCont).DataPropertyName = "FALSE" Then
                                sSQL = sSQL & IIf(sSQL = "", "", ",") & "0 as FALSE"
                            ElseIf .Columns.Item(sCont).DataPropertyName = "TRUE" Then
                                sSQL = sSQL & IIf(sSQL = "", "", ",") & "1 as TRUE"
                            ElseIf .Columns.Item(sCont).DataPropertyName = "Baixar" Then
                                sSQL = sSQL & IIf(sSQL = "", "", ",") & "'Baixar' as Baixar"
                            ElseIf .Columns.Item(sCont).ToolTipText <> "" Then
                                sSQL = sSQL & IIf(sSQL = "", "", ",") & .Columns.Item(sCont).ToolTipText & " as " & .Columns.Item(sCont).DataPropertyName
                            ElseIf .Columns.Item(sCont).DataPropertyName <> "" Then
                                sSQL = sSQL & IIf(sSQL = "", "", ",") & .Columns.Item(sCont).DataPropertyName
                            End If
                        Next
                        If parFrom.Trim.Substring(0, 5).ToUpper <> "FROM " Then parFrom = " FROM " & parFrom
                        sSQL = "SELECT " & sSQL & parFrom & IIf(parCondicao = "", "", IIf(InStr(parCondicao, "WHERE") > 0, "", " WHERE ") & parCondicao)
                    End With
                Else
                    sSQL = parSQL
                End If

                If parAutomatico Then
                    sDyna.Open(sSQL, fBanco, 1, 2)
                    Dim da As New OleDbDataAdapter
                    Dim ds As New DataSet
                    da.Fill(ds, sDyna, "TABELA")
                    With parGrid
                        .DataSource = ds.Tables("TABELA")
                        .Refresh()
                        ds = Nothing
                        da = Nothing
                    End With
                Else
                    sDyna.Open(sSQL, fBanco, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly)
                    With parGrid
                        If Not sDyna.EOF Then
                            .RowCount = sDyna.RecordCount
                            While Not sDyna.EOF
                                For Each sField As ADODB.Field In sDyna.Fields
                                    .Rows(sDyna.AbsolutePosition - 1).Cells(sField.Name).Value = sField.Value
                                Next
                                sDyna.MoveNext()
                            End While
                        End If
                    End With
                End If

                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            Return False
        Finally
            fProcessando = False
            Libera(sDyna)
            If parConecta Then Desconecta()
        End Try
    End Function


    'Public Function getRecordset(ByVal parConsulta As String) As ADODB.Recordset
    '    Dim sSql As String, sDyna As New ADODB.Recordset
    '    Try
    '        With fBanco
    '            .Provider = "SQLOLEDB"
    '            .Properties("Data Source").Value = pClsPDVINT.Servidor
    '            .Properties("Persist Security Info").Value = True
    '            .Properties("Initial Catalog").Value = pClsPDVINT.Banco
    '            .Properties("User Id").Value = pClsPDVINT.Usuario
    '            .Properties("Password").Value = pClsPDVINT.Senha
    '        End With
    '        sSql = parConsulta
    '        fBanco.
    '        sDyna.Open(sSql, fBanco, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)
    '        sDyna.Close()
    '        Return sDyna
    '    Catch ex As Exception
    '        MsgBox(Err.Description, vbCritical, "getRecordset")
    '        Return Nothing
    '    Finally
    '        sDyna = Nothing
    '        fBanco = Nothing
    '    End Try
    'End Function
End Class



Public Class ComboItem

    Private mid As Integer
    Private mdescricao As String

    Public Sub New(ByVal id As Integer, ByVal descricao As String)
        mid = id
        mdescricao = descricao
    End Sub

    Public Property ID() As Integer
        Get
            Return mid
        End Get

        Set(ByVal value As Integer)
            mid = value
        End Set
    End Property

    Public Property Descricao() As String
        Get
            Return mdescricao
        End Get

        Set(ByVal value As String)
            mdescricao = value
        End Set
    End Property


End Class
